package com.zmn.oms.common.constant;

import com.google.common.collect.Lists;
import com.zmn.common.dictionary.DictModel;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：工单状态常量
 * @author heciqi
 * @date 2020/4/17 10:23
 */
public class OrderStatusConsts {

    /**
     * 订单状态
     */
    public final static int ORDER_STATUS_INPUT = 11; // 提交订单
    public final static int ORDER_STATUS_CONFIRM = 21; // 确认订单
    public final static int ORDER_STATUS_DISTRIBUTE = 41; // 成功派单
    public final static int ORDER_STATUS_TAKE = 51; // 师傅接单
    public final static int ORDER_STATUS_VISIT = 61; // 服务上门
    public final static int ORDER_STATUS_COMPLETE = 71; // 订单完成
    public final static int ORDER_STATUS_CANCEL = 81; // 服务取消

    /**
     * 订单/工单结果
     */
    public final static int WORK_RESULT_DOING = 1; // 进行中
    public final static int WORK_RESULT_SUCCESS = 2; // 成功
    public final static int WORK_RESULT_FAIL = 3; // 失败
    public final static int WORK_RESULT_DISCARD = 4; // 弃单

    /**
     * 工单状态
     */
    public final static int WORK_STATUS_RECEIVE = 8; // 已创建 （暂时没用）
    public final static int WORK_STATUS_INPUT = 11; // 已下单
    public final static int WORK_STATUS_CONFIRM = 31; // 已确认 （平台确认）
    public final static int WORK_STATUS_ASSIGN = 34; // 已分单
    public final static int WORK_STATUS_DISTRIBUTE = 41; // 已派单
    public final static int WORK_STATUS_TAKE = 51; // 已领单
    public final static int WORK_STATUS_VISIT = 61; // 已上门
    public final static int WORK_STATUS_SERVICE_COMPLETE = 66; // 服务完成
    public final static int WORK_STATUS_COMPLETE = 71; // 已完成
    public final static int WORK_STATUS_CHECKOUT = 81; // 已收单
    public final static int WORK_STATUS_ACCOUNT = 91; // 已算账

    /**
     * 订单状态
     *
     * @return
     */
    public static List<DictModel> getOrderStatusList() {
        List<DictModel> items = Lists.newArrayList();
        items.add(new DictModel(ORDER_STATUS_INPUT, "已录入"));
        items.add(new DictModel(ORDER_STATUS_CONFIRM, "已确认"));
        items.add(new DictModel(ORDER_STATUS_DISTRIBUTE, "已派单"));
        items.add(new DictModel(ORDER_STATUS_TAKE, "已领单"));
        items.add(new DictModel(ORDER_STATUS_VISIT, "已上门"));
        items.add(new DictModel(ORDER_STATUS_COMPLETE, "已完成"));
        items.add(new DictModel(ORDER_STATUS_CANCEL, "已取消"));
        return items;
    }

    /**
     * 工单状态
     *
     * @return
     */
    public static List<DictModel> getWorkStatusList() {
        List<DictModel> items = Lists.newArrayList();
        items.add(new DictModel(WORK_STATUS_RECEIVE, "已创建"));
        items.add(new DictModel(WORK_STATUS_INPUT, "已录入"));
        items.add(new DictModel(WORK_STATUS_CONFIRM, "已确认"));
        items.add(new DictModel(WORK_STATUS_ASSIGN, "已分单"));
        items.add(new DictModel(WORK_STATUS_DISTRIBUTE, "已派单"));
        items.add(new DictModel(WORK_STATUS_TAKE, "已领单"));
        items.add(new DictModel(WORK_STATUS_VISIT, "已上门"));
        items.add(new DictModel(WORK_STATUS_SERVICE_COMPLETE, "服务完成"));
        items.add(new DictModel(WORK_STATUS_COMPLETE, "已完成"));
        items.add(new DictModel(WORK_STATUS_CHECKOUT, "已收单"));
        items.add(new DictModel(WORK_STATUS_ACCOUNT, "已算账"));
        return items;
    }

    /**
     * 工单结果状态
     *
     * @return
     */
    public static List<DictModel> getOrderResultStatusList() {
        List<DictModel> items = Lists.newArrayList();
        items.add(new DictModel(WORK_RESULT_DOING, "进行中"));
        items.add(new DictModel(WORK_RESULT_SUCCESS, "成功"));
        items.add(new DictModel(WORK_RESULT_DISCARD, "弃单"));
        items.add(new DictModel(WORK_RESULT_FAIL, "失败"));
        return items;
    }

    /**
     * 工单结果状态 进行中、成功、弃单
     *
     * @return
     */
    public static List<DictModel> getYsOrderResultStatusList() {
        List<DictModel> items = Lists.newArrayList();
        items.add(new DictModel(WORK_RESULT_DOING, "进行中"));
        items.add(new DictModel(WORK_RESULT_SUCCESS, "成功"));
        items.add(new DictModel(WORK_RESULT_DISCARD, "弃单"));
        return items;
    }

    /**
     * 查询工单状态名称
     *
     * @param status 工单状态
     * @return 工单状态名称
     */
    public static String getWorkStatusName(int status) {

        switch (status) {
            case WORK_STATUS_RECEIVE:
                return "已创建";
            case WORK_STATUS_INPUT:
                return "已录入";
            case WORK_STATUS_CONFIRM:
                return "已确认";
            case WORK_STATUS_ASSIGN:
                return "已分单";
            case WORK_STATUS_DISTRIBUTE:
                return "已派单";
            case WORK_STATUS_TAKE:
                return "已领单";
            case WORK_STATUS_VISIT:
                return "已上门";
            case WORK_STATUS_SERVICE_COMPLETE:
                return "服务完成";
            case WORK_STATUS_COMPLETE:
                return "已完成";
            case WORK_STATUS_CHECKOUT:
                return "已收单";
            case WORK_STATUS_ACCOUNT:
                return "已算账";
            default:
                return "未知";
        }
    }

    /**
     * 查询工单状态名称
     *
     * @param status 工单状态
     * @return 工单状态名称
     */
    public static String getOrderStatusName(int status) {

        switch (status) {
            case ORDER_STATUS_INPUT:
                return "提交订单";
            case ORDER_STATUS_CONFIRM:
                return "确认订单";
            case ORDER_STATUS_DISTRIBUTE:
                return "成功派单";
            case ORDER_STATUS_TAKE:
                return "师傅接单";
            case ORDER_STATUS_VISIT:
                return "服务上门";
            case ORDER_STATUS_COMPLETE:
                return "服务完成";
            case ORDER_STATUS_CANCEL:
                return "服务取消";
            default:
                return "未知";
        }
    }

    /**
     * 查询工单结果状态名称
     *
     * @param resultStatus 工单结果状态
     * @return 工单结果状态名称
     */
    public static String getWorkResultStatusName(Integer resultStatus) {

        switch (resultStatus) {
            case WORK_RESULT_DOING:
                return "进行中";
            case WORK_RESULT_SUCCESS:
                return "成功";
            case WORK_RESULT_FAIL:
                return "失败";
            case WORK_RESULT_DISCARD:
                return "弃单";
            default:
                return "未知";
        }
    }

    /**
     * 根据工单信息获取订单状态
     * @param workStatus 工单状态
     * @param workResultStatus
     * @return
     */
    public static int getOrderStatus(Integer workStatus, Integer workResultStatus) {
        if (workStatus > WORK_STATUS_COMPLETE) {
            return ORDER_STATUS_COMPLETE;
        }

        if (Objects.equals(workResultStatus, WORK_RESULT_FAIL)
                || Objects.equals(workResultStatus, WORK_RESULT_DISCARD)) {
            return ORDER_STATUS_CANCEL;
        }

        // 工单状态---订单状态，映射
        // 已下单（11） ==> 提交订单（11）；
        // 已确认（31），已分单（34） ==> 确认订单（21）
        // 已派单（41） ==> 成功派单（41）
        // 已领单（51） ==> 师傅接单（51）
        // 已上门（61）, 服务完成（66）  ==> 服务上门（61）
        // 已完成（71），已收单（81），已算账（91） ==> 服务完成（71）

        switch (workStatus) {
            case WORK_STATUS_INPUT:
                return ORDER_STATUS_INPUT;
            case WORK_STATUS_CONFIRM:
            case WORK_STATUS_ASSIGN:
                return ORDER_STATUS_CONFIRM;
            case WORK_STATUS_DISTRIBUTE:
                return ORDER_STATUS_DISTRIBUTE;
            case WORK_STATUS_TAKE:
                return ORDER_STATUS_TAKE;
            case WORK_STATUS_SERVICE_COMPLETE:
            case WORK_STATUS_VISIT:
                return ORDER_STATUS_VISIT;
            case WORK_STATUS_COMPLETE:
            case WORK_STATUS_CHECKOUT:
            case WORK_STATUS_ACCOUNT:
                return ORDER_STATUS_COMPLETE;
            default:
                return ORDER_STATUS_CANCEL;
        }
    }

    /**
     * 获取工单状态
     * @return
     */
    public static List<DictModel> getZsWorkStatus() {
        List<DictModel> workStatusList = getWorkStatusList();
        return workStatusList.stream().filter(e -> !Objects.equals(e.getKey(), WORK_STATUS_RECEIVE)).collect(Collectors.toList());
    }

}